﻿@model  DHQGHN.DAL.Common.DataPage<DHQGHN.PM0.Models.Admin.RoleModel>
@{
    var pagNar = ViewBag.Pagination as DHQGHN.DAL.Common.Pagination;
    var lstItem = Model.Data;
}
<table class="tbl">
    <thead>
        <tr>
            <th style="width:5%">STT</th>
            <th style="width:5%">Chọn</th>
            <th style="width:40%">Tên vai trò</th>
            <th style="width:20%">Mã vai trò</th>
            <th style="width:25%">Mô tả</th>
            <th style="width:5%">Sửa</th>
        </tr>
    </thead>
    <tbody id="ctg_content_body">
        @if (Model.Data.Any())
        {
            for (int i = 0; i < lstItem.Count; i++)
            {
                <tr>
                    <td class="align-center">@(i + 1 + pagNar.CurrentPage * pagNar.PageSize)</td>
                    <td style="text-align:center"><div id="checkboxRole"><input type="checkbox" name="@lstItem[i].RoleId" /></div></td>
                    <td style="text-align: left;">@lstItem[i].RoleName</td>
                    <td style="text-align: left;">@lstItem[i].RoleCode</td>
                    <td style="text-align: left;">@lstItem[i].RoleDescription</td>
                    <td style="text-align: center;">
                        <a title='Sửa thông tin' href='javascript:void(0)' class='role-edit' data-id='@lstItem[i].RoleId'>
                            <div class='glyphicon glyphicon-edit'></div>
                        </a>
                    </td>
                </tr>
            }
        }
        else
        {
            <tr>

                <td colspan="6"><p>Không tìm thấy vai trò.</p></td>
            </tr>
        }
    </tbody>
    <tfoot>

        <tr>
            <td colspan="6" style="text-align:left;height:25px">
                <button type="button" class="btn btn-danger" title="Xóa vai trò" id="btnDelete">Xóa</button>
            </td>
        </tr>

        <tr>
            <td colspan="6">
                @if (Model.Data.Any())
                {
                    @Html.Partial("_PaginationView", pagNar)
                }

            </td>
        </tr>
    </tfoot>
</table>
<div id="navigate_bottom_panel" class="nav-bottom ">
</div>
<div id="role-model-container"></div>

<script>
        @functions{
            public string GetAntiForgeryToken()
            {
                string cookieToken, formToken;
                AntiForgery.GetTokens(null, out cookieToken, out formToken);
                return cookieToken + "," + formToken;
            }
        }
</script>
<script type="text/javascript">

    var crrPageIndex
    function OnCriteria(pageIndex) {
        var name = $('#input-name');
        var code = $('#input-code');
        var appid = $('#input-appid').val();

        crrPageIndex = pageIndex;

        name = name != null ? name.val().trim() : "";
        code = code != null ? code.val().trim() : "";
        $.ajax({
            dataType: "html",
            type: "POST",
            url: '/Role/actionSearch',
            contentType: "application/json",
            data: JSON.stringify({ pageIndex: pageIndex, aModel: { RoleName: name, RoleCode: code, AppId: appid } }),
            success: function (response) {
                var $catlist = $('#role-list');

                if ($catlist == null || $catlist.length == 0) {
                    window.notice('@*@Resources.Localization.MsgItemNotFound*@', window.notice_error);
                }
                else {
                    $catlist.html(response);
                }
            }
        });
        setTimeout(function () { $('#search_form').find('input:text:visible:first').focus(); }, 500);
    }


    $(document).ready(function () {
        $('a').tipsy();

        $("table.tbl-user-list").colResizable({
            headerOnly: true,
            defaultWith: ["auto", "auto", "auto", "auto", "60px", '30px', '30px', '30px']
        });

        var dialog;
        $('.role-edit').bind('click', function (event) {
            functionCreateOrUpdate($(this).attr('data-id'), "Cập nhật vai trò");
        });

        $('#btnDelete').click(function () {
            var values = [];
            $('#checkboxRole input:checked').each(function () {
                values.push(this.name);
            });

            if (values.length != 0) {
                var $self = $(this);
                comfirmDelete("Bạn có muốn xóa vai trò không?", function () {
                    $.ajax("/Role/actionDelete", {
                        type: "post",
                        contentType: "application/json",
                        data: JSON.stringify({ roleList: values }), // JSON data goes here
                        dataType: "json",
                        headers: {
                            'VerificationToken': '@GetAntiForgeryToken()'
                        },
                        success: function (response) {
                            if (response.status == 200) {
                                OnCriteria(0);
                                window.notice(response.result, window.notice_success);
                            }
                            else {
                                window.notice(response.result, window.notice_error);
                            }
                        },
                    });
                }, null, "Xóa vai trò");
            } else {
                warning("Cần chọn vai trò để xóa", "Xóa vai trò");
            }
        });
    });
</script>
